<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/validate.jsp"%>
<%
    String deptId = input.getDesc("dept_id", "");
    String expertId = input.getDesc("expert_id", "");
    String branch = input.getDesc("branch", "");
    String registerType = input.getDesc("type", "");
    String selectContent = "new map(id as id, doctorId as doctor_id, doctorName as doctor_name, deptId as dept_id, deptName as dept_name, date as date, half as half, registerType as register_type, registerName as register_name, workStatus as work_status, registryFee as registry_fee, branch as branch, branchName as branch_name)";
    Where where = new Where();
    if (!"".equals(branch)) {
        where.and("branch=", branch);
    }
    if (!"".equals(expertId)) {
        where.and("doctorId=", expertId);
        where.and("registerType=", 2);
    } else {
        where.and("deptId=", deptId);
        where.and("registerType=", 1);
    }
    if (!"".equals(registerType)) {
        where.and("registerType=", registerType);
    }
    List<Map> shcedules = QBuilder.db("basedb").select(selectContent).from("Schedule").where(where.and("hospital =", hospital).and("status = ", STATUS_ENABLE)).query().getResultObj();
    if (shcedules.size() == 0) {
        msg = new Message(0, "没有相关排班信息");
    }
    Date d = new Date();
    List<Map> result = new ArrayList<Map>();
    for (int i = 0; i <= 14; i++) {
        Date date = DateUtil.afterNDay(d, i);
		String year = DateUtil.formatDateByFormat(date, "yyyy-MM-dd");
		String day = DateUtil.formatDateByFormat(date, "MM-dd");
		String week = DateUtil.formatDateByFormat(date, "EEEE").replaceAll("星期", "周");
        Map map = new HashMap();
        result.add(map);
        map.put("day", day);
        map.put("week", week);
        List<Map> am = (List<Map>)map.get("am");
        if (am == null) {
            am = new ArrayList<Map>();
            map.put("am", am);
        }
        List<Map> pm = (List<Map>)map.get("pm");
        if (pm == null) {
            pm = new ArrayList<Map>();
            map.put("pm", pm);
        }
        List<Map> whole = (List<Map>)map.get("whole");
        if (whole == null) {
            whole = new ArrayList<Map>();
            map.put("whole", whole);
        }
        List<Map> night = (List<Map>)map.get("night");
        if (night == null) {
            night = new ArrayList<Map>();
            map.put("night", night);
        }
        if (shcedules.size() == 0) {
            continue;
        }
        Iterator<Map> it = shcedules.iterator();
		while(it.hasNext()) {
			Map _map = it.next();
            String _date = Tools.getDesc(_map.get("date"), "----");
            String half = Tools.getDesc(_map.get("half"), "----");
            if (_date.equals(year)) {
                if ("1".equals(half)) {
                    am.add(_map);
                } else if ("2".equals(half)) {
                    pm.add(_map);
                } else if ("3".equals(half)) {
                    whole.add(_map);
                } else if ("4".equals(half)) {
                    night.add(_map);
                }
                it.remove();
            }
		}
    }
    msg.put("data", result);
    Tools.encryptPrint(out, msg);
%>
